<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%@ include file="/head.jsp" %>

<body>


    <div class="layui-form layui-form-pane" style="float: right;margin: 5px;">
        <button style="display: none" class="layui-btn" data-type="reload">添加课程</button>
        <div class="layui-input-inline">
            <select name="stIdOne" id="stIdOne" lay-filter="one">
                <option value="">请选择一级分类</option>
                <c:forEach items="${typeList}" var="t">
                    <option value="${t.stId}">${t.stName}</option>
                </c:forEach>
            </select>
        </div>

        <div class="layui-input-inline">
            <select name="stIdTwo" id="stIdTwo" lay-filter="two">
            </select>
        </div>

        <div class="layui-input-inline">
            <select name="stIdThree" id="stIdThree" lay-filter="three">
            </select>
        </div>

        <div class="layui-input-inline">
            <select name="checkStatus1"  id="checkStatus1" lay-filter="checkStatus1">
                <option value="-1">请选择审核状态</option>
                <option value="0">审核中</option>
                <option value="1">已通过</option>
                <option value="2">不通过</option>
            </select>
        </div>

        <div class="layui-input-inline">
            <select name="isDel1"  id="isDel1" lay-filter="isDel1">
                <option value="-1">请选择上下架状态</option>
                <option value="0">上架</option>
                <option value="1">下架</option>
            </select>
        </div>

        <div class="layui-input-inline">
            <input type="text" name="videoName" id="videoName"  lay-verify="required"
                   placeholder="请输入视频名称" autocomplete="off" class="layui-input">
        </div>
        <button class="layui-btn" data-type="reload">搜索</button>
    </div>
<%--    清除上面的浮动对下面布局的影响--%>
    <div style="clear: both;"></div>
    <div class="layui-form layui-form-pane" style="margin: 5px;">
        <table id="demo" lay-filter="test"></table>
    </div>

<%--序号--%>
<script type="text/html" id="increase">
    {{d.LAY_TABLE_INDEX+1}}
</script>

<script type="text/html" id="createTime">
    {{# return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss') }}
</script>

<script type="text/html" id="upOrDown">
    {{# if (d.upOrDown=== 0) { }}
        下架
    {{# } else if(d.upOrDown=== 1) { }}
        上架
    {{# } else { }}

    {{# } }}
</script>

<script type="text/html" id="isFree">
    {{# if (d.isFree=== 0) { }}
        免费
    {{# } else if(d.isFree=== 1) { }}
        收费
    {{# } else { }}

    {{# } }}
</script>
<script type="text/html" id="isPublish">
    {{# if (d.isPublish=== 0) { }}
        未发布
    {{# } else if(d.isPublish=== 1) { }}
        发布
    {{# } else { }}

    {{# } }}
</script>
<script type="text/html" id="isRecomment">
    {{# if (d.isRecomment=== 0) { }}
        否
    {{# } else if(d.isRecomment=== 1) { }}
        是
    {{# } else { }}

    {{# } }}
</script>
<script type="text/html" id="checkStatus">
    {{# if (d.checkStatus=== 0) { }}
        审核中
    {{# } else if(d.checkStatus=== 1) { }}
        已通过
    {{# } else if(d.checkStatus=== 2) { }}
        不通过
    {{# } else { }}

    {{# } }}
</script>


<script type="text/html" id="videoImg">
    <!--使用viewer插件进行渲染-->
    <ul class="showImg">
        {{#  layui.each(d.videoImg.split(','), function(index, item){ }}
        <li><img src="${url}{{item}}" /></li>
        {{#  }); }}
    </ul>
</script>

 <script type="text/html" id="recomment">
     <input type="checkbox" name="recomment" value="{{d.videoId}}" title="加入推荐" lay-filter="recomment" {{ d.isRecomment == 1 ? 'checked' : '' }} />
 </script>

<script type="text/html" id="barDemo">
    <c:if test="${model.glDelete != 1} && ${model.glUpdate !=1}">
        没有权限
    </c:if>
    <a class="layui-btn layui-btn-xs" lay-event="goSellOrders">出售订单</a>
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="addVideoInfo">添加资料</a>
    {{# if (d.checkStatus=== 0) { }}
        <a class="layui-btn layui-btn-xs" lay-event="pass">通过</a>
        <a class="layui-btn layui-btn-xs" lay-event="noPass">不通过</a>
    {{# } }}

    <a class="layui-btn layui-btn-xs" lay-event="goCheckVideoItem">审核子视频</a>
    <c:if test="${model.glUpdate == 1}">
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="edit">修改分类</a>
<%--        {{# if (d.isPublish=== 1) { }}--%>
<%--            <a class="layui-btn layui-btn-xs" lay-event="tryVideoItemId">指定试看视频</a>--%>
<%--        {{# } }}--%>

    </c:if>

    {{# if (d.isDel === 0) { }}
        <a class="layui-btn layui-btn-xs" lay-event="del">下架</a>
    {{# } else { }}
        <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="up">上架</a>
    {{# } }}

</script>


<script>
    layui.use(['util', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'form'], function () {
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery,//加载jquery对象
            layer = layui.layer;

        //让层自适应iframe
        $('#add').on('click', function () {
            var index = layer.open({
                title: '信息',
                type: 2,
                content: '/manage/videos/goAdd',
                area: ['800px', '450px'],
                maxmin: true,
                end: function () {
                    location.reload();
                }
            });
            parent.layer.iframeAuto(index);
        });

        //监听锁定操作
        form.on('checkbox(recomment)', function(obj){
            var isRecomment = 0;
            if (obj.elem.checked==true){
                isRecomment = 1;
            }

            $.ajax({
                url: '/manage/videos/updateType',
                type: 'post',
                dataType: 'json',
                data: {isRecomment: isRecomment,videoId : obj.value},
                success: function (data) {
                    if (data.code == "200") {
                        layer.msg(data.msg, {icon: 1, time: 500}, function () {
                            window.location.reload();
                        });
                    } else {
                        layer.msg(data.msg, {icon: 1, time: 1000});
                    }
                }
            })
            return false;
        });

        //第一个实例
        table.render({
            elem: '#demo'
            , url: '/manage/videos/findAllJson' //数据接口
            , page: true //开启分页
            , limit: 10 //初始页条数
            , cols: [[ //表头
                 {field: 'id', title: 'ID', width: 80, templet: '#increase'}
                , {field: 'videoId',hide:true, title: '课程Id', width: 100}
                , {field: 'videoName', title: '课程名称', width: 100}
                , {field: 'videoImg', title: '课程封面图', width: 150, templet: '#videoImg'}
                , {field: 'videoDesc', title: '课程介绍', width: 100}
                , {field:'recomment', title:'加入推荐', width:150, templet: '#recomment', unresize: true,sort:true}
                , {field:'checkStatus', title:'状态', width:150, templet: '#checkStatus', unresize: true,sort:true}
                , {field:'noPassReason', title:'上次审核不通过原因', width:180}
                , {field: 'isPublish', title: '是否发布', width: 100, templet: '#isPublish',sort:true}
                , {field: 'createTime', title: '上传日期', width: 200, templet: '#createTime',sort: true}
                , {field: 'stIdOneName', title: '一级分类名称', width: 150}
                , {field: 'stIdTwoName', title: '二级分类名称', width: 150}
                , {field: 'stIdThreeName', title: '三级分类名称', width: 150}
                , {field: 'isFree', title: '是否收费', width: 100,templet: '#isFree'}
                , {field: 'teacherName', title: '讲师名字', width: 100}
                , {field: 'tryVideoItemId',hide:true, title: '试看视频id', width: 100}
                , {field: 'currentPrice', title: '售价', width: 100,sort: true}
                , {field: 'saleCount', title: '销量', width: 100,sort: true}
                , {fixed: 'right', title: '操作栏', width: 500, align: 'left', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
            ]]
            , done: function () {
                $(".showImg").viewer();
            }
            , id: 'testReload' //重载标识
        });
        //监控select
        form.on('select(one)',function (data) {
            var stFId = data.value;
            $.ajax({
                url: '/manage/type/findTypeByStFid',
                type: 'post',
                dataType: 'json',
                data: {stFId: stFId},
                success: function (data) {
                    var array = data.data;
                    var stIdTwo = $("#stIdTwo");
                    var stIdThree = $("#stIdThree");
                    stIdTwo.html("");
                    stIdThree.html("");
                    var str ='';
                    str += "<option value=''>" + '请选择二级分类' +"</option>";
                    for (var i = 0; i < array.length; i++) {
                        str += "<option value='"+ array[i].stId +"'>" + array[i].stName +"</option>";
                    }
                    stIdTwo.append(str);
                    form.render('select');
                }
            })
        })

        form.on('select(two)',function (data) {
            var stFId = data.value;
            $.ajax({
                url: '/manage/type/findTypeByStFid',
                type: 'post',
                dataType: 'json',
                data: {stFId: stFId},
                success: function (data) {
                    var array = data.data;
                    var stIdThree = $("#stIdThree");
                    stIdThree.html("");
                    var str ='';
                    str += "<option value=''>" + '请选择三级分类' +"</option>";
                    for (var i = 0; i < array.length; i++) {
                        str += "<option value='"+ array[i].stId +"'>" + array[i].stName +"</option>";
                    }
                    stIdThree.append(str);
                    form.render('select');
                }
            })
        })


        var active = {
            // 方法级渲染的重载 不需要 #
            reload: function () {
                //获取条件
                var videoName = $('#videoName').val();
                var stIdOne = $('#stIdOne').val();
                var stIdTwo = $('#stIdTwo').val();
                var stIdThree = $('#stIdThree').val();
                var checkStatus = $('#checkStatus1').val();
                var isDel = $('#isDel1').val();

                //执行重载 testReload 对应第一个实例中 id
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        videoName: videoName,
                        stIdOne: stIdOne,
                        stIdTwo: stIdTwo,
                        stIdThree: stIdThree,
                        checkStatus : checkStatus,
                        isDel : isDel
                    }
                });
            }
            // 获取checkStatus行数据
            , dels: function () { //获取选中数据 demo 为table的id
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var userIds = [];
                data.forEach(function (item, index) {
                    userIds.push(item.userId);
                });
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/manage/videos/dels',
                        type: 'post',
                        dataType: 'json',
                        data: {userIds: userIds.join(",")},//join 将数组组装成 1,2,3 的形式
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }
            , update: function () { //获取选中数据 demo 为table的id   data
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg("Select an action");
                    return false;
                }
                //data 获取的是数组,所以当修改的时候就要获取第一个数据
                var userId = data[0].userId;
                var index = layer.open({
                    title: '信息',
                    type: 2,
                    content: '/manage/videos/goEdit?userId=' + userId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            }
        };

        $('.layui-form .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'noPass') { //不通过
                var index = layer.open({
                    title: '审核',
                    type: 2,
                    content: '/manage/videos/toNoPass?videoId=' + data.videoId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            }else if (layEvent === 'addVideoInfo' ) { //添加资料
                // if(data.userDesc == null || data.userDesc == ""){
                //     alert("请添加讲师简介");
                //     return;
                // }
                layer.open({
                    title: '上传资料(只允许上传压缩文件)',
                    type: 2,
                    content: '/manage/webUser/uploadVideoInfo?userId='+data.userId+'&videoId='+data.videoId,
                    area: ['1100px', '650px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
            }else if (layEvent === 'pass') { //删除
                layer.confirm('真的通过吗?', function (index) {
                    $.ajax({
                        url: '/manage/videos/pass',
                        type: 'post',
                        dataType: 'json',
                        data: {videoId: data.videoId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1500});
                            }
                        }
                    })
                });
            }else if (layEvent === 'goSellOrders') {
                window.location.href = '/manage/videos/findOrdersByPublishVideoId?videoId='+data.videoId
            } else if (layEvent === 'goCheckVideoItem') { //审核视频
                window.location.href = '/manage/videosItem/findAllByVideoId?videoId='+data.videoId
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的下架吗?', function (index) {
                    $.ajax({
                        url: '/manage/videos/del',
                        type: 'post',
                        dataType: 'json',
                        data: {videoId: data.videoId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }else if (layEvent === 'up') { //删除
                layer.confirm('真的上架吗?', function (index) {
                    $.ajax({
                        url: '/manage/videos/up',
                        type: 'post',
                        dataType: 'json',
                        data: {videoId: data.videoId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }else if (layEvent === 'edit') { //修改分类tryVideoItemId
                var index = layer.open({
                    title: '编辑',
                    type: 2,
                    content: '/manage/videos/goEdit?videoId=' + data.videoId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            } else if (layEvent === 'tryVideoItemId') { //修改指定试看视频
                var index = layer.open({
                    title: '修改指定试看视频',
                    type: 2,
                    content: '/manage/videos/findVideoItemsByVideoId?videoId=' + data.videoId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            } else if (layEvent === 'openImg') { //监听单元格
                //页面层-展示图片
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: ['450px', '450px'],
                    skin: 'layer-ext-myskin', //没有背景色
                    shadeClose: true,
                    content: '<img src="${url}' + data.userImg + '" height="450px" width="450px" alt="Load failed"/>'
                });
            }
        });

        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('testReload')
                , data = checkStatus.data;
            $.each(data, function (index, item) {
                ids.push(item['userId'])
            });
            return ids;
        }

    });
</script>

<%@ include file="/foot.jsp" %>
